package com.untrance.shop.core.dao.bean;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.google.appengine.api.datastore.Key;
import com.untrance.shop.common.utils.JpaUtils;
import com.untrance.shop.core.dao.ImageDao;
import com.untrance.shop.model.Image;

/**
 * @author eugene zadyra
 */
@Repository("imageDao")
@Transactional(readOnly=true)
public class ImageDaoBean extends MainDaoBean<Image> implements ImageDao {
	
	public ImageDaoBean() {
		super(Image.class);
	}

	@Override
	public Image findImageByName(String name) {
		Query query = em.createQuery("SELECT i FROM Image i" +
				" WHERE i.name=:name"
				);
		query.setParameter("name", name);
		return JpaUtils.getSingleResult(query);
	}
	
	@Override
	public Key findImageKeyByName(String name) {
		Query query = em.createQuery("SELECT i.id FROM Image i" +
				" WHERE i.name=:name"
				);
		query.setParameter("name", name);
		return JpaUtils.getSingleResult(query);
	}

	@Override
	public List<Key> findAdditionalImagesByProductKey(Key productKey) {
		Query query = em.createQuery("SELECT i.additionalImages FROM Product i" +
				" WHERE i.id=:key"
				);
		query.setParameter("key", productKey);
		return JpaUtils.getCheckedResultList(query);
	}
}
